package com.bestescortbedsystem.service.impl;

import com.bestescortbedsystem.dao.*;
import com.bestescortbedsystem.entity.*;
import com.bestescortbedsystem.service.BedService;
import com.bestescortbedsystem.util.FindDate;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * ClassName: BedServiceImpl
 * Package: com.bestescortbedsystem.service.impl
 *
 * @author: guanzepeng
 * @Date: 2021/2/26 14:24 星期五
 * @Description: 床位信息业务层
 */
@Service
public class BedServiceImpl implements BedService {

	@Resource
	private SesBedMessageDao sesBedMessageDao;

	@Resource
	private SesBedTypeDao sesBedTypeDao;

	@Resource
	private SesHospitalinfoDao sesHospitalinfoDao;

	@Resource
	private SesHospitalinfoDepartmentDao sesHospitalinfoDepartmentDao;

	@Resource
	private SesUserDao sesUserDao;

	/**
	 * 查询床位的订单信息
	 * @param pageNum 从第几条数据开始分页
	 * @param pageSize 第几页
	 * @param hospitalinfoId 传入医院ID
	 * @param bedtypeid 传入床位ID
	 * @param hospitalinfoDepartmentid 传入科室ID
	 *
	 * @return 返回Map集合
	 */
	@Override
	public Map<String, Object> findBedMessage(Integer pageNum, Integer pageSize, Long hospitalinfoId, Long bedtypeid, Long hospitalinfoDepartmentid) {
		SesBedMessageExample sesBedMessageExample = new SesBedMessageExample();
		SesBedMessageExample.Criteria criteria = sesBedMessageExample.createCriteria();
		if(hospitalinfoId != 0) {
			criteria.andHospitalinfoidEqualTo(hospitalinfoId);
		}
		if(bedtypeid != 0) {
			criteria.andBedBedTypeidEqualTo(bedtypeid);
		}
		if(hospitalinfoDepartmentid != 0) {
			criteria.andHospitalinfoDepartmentidEqualTo(hospitalinfoDepartmentid);
		}
		Map<String, Object> map = new HashMap<>();
		PageHelper.startPage(pageNum, pageSize);
		List<SesBedMessage> sesBedMessages = sesBedMessageDao.selectByExample(sesBedMessageExample);
		for (SesBedMessage s : sesBedMessages) {
			SesBedType sesBedType = sesBedTypeDao.selectByPrimaryKey(s.getBedBedTypeid());
			s.setSesBedType(sesBedType);
			SesHospitalinfo sesHospitalinfo = sesHospitalinfoDao.selectByPrimaryKey(s.getHospitalinfoid());
			s.setSesHospitalinfo(sesHospitalinfo);
			SesHospitalinfoDepartment sesHospitalinfoDepartment = sesHospitalinfoDepartmentDao.selectByPrimaryKey(s.getHospitalinfoDepartmentid());
			s.setSesHospitalinfoDepartment(sesHospitalinfoDepartment);
		}
		PageInfo<SesBedMessage> pageInfo = new PageInfo<>(sesBedMessages);
		map.put("chuang", pageInfo);

		List<SesBedType> sesBedTypesList = sesBedTypeDao.selectByExample(null);
		map.put("sesBedTypesList",sesBedTypesList);

		List<SesHospitalinfo> sesHospitalinfoList = sesHospitalinfoDao.selectByExample(null);
		map.put("sesHospitalinfoList", sesHospitalinfoList);

		List<SesHospitalinfoDepartment> sesHospitalinfoDepartmentList = sesHospitalinfoDepartmentDao.selectByExample(null);
		map.put("sesHospitalinfoDepartmentList",sesHospitalinfoDepartmentList);

		//查询所有股东
		SesUserExample sesUserExample = new SesUserExample();
		sesUserExample.createCriteria().andUsertypeEqualTo(4);
		List<SesUser> sesUserList = sesUserDao.selectByExample(sesUserExample);
		map.put("gudong", sesUserList);

		//查询所有维修人员
		SesUserExample sesUserExample5 = new SesUserExample();
		sesUserExample5.createCriteria().andUsertypeEqualTo(2);
		List<SesUser> sesUserList2 = sesUserDao.selectByExample(sesUserExample5);
		map.put("weixiu", sesUserList2);

		//查询所有一级分销商
		SesUserExample sesUserExample1 = new SesUserExample();
		sesUserExample1.createCriteria().andUsertypeEqualTo(10);
		List<SesUser> sesUserList1 = sesUserDao.selectByExample(sesUserExample1);
		map.put("yiji", sesUserList1);

		//查询所有二级分销商
		SesUserExample sesUserExample2 = new SesUserExample();
		sesUserExample2.createCriteria().andUsertypeEqualTo(11);
		List<SesUser> sesUserList3 = sesUserDao.selectByExample(sesUserExample2);
		map.put("erji", sesUserList3);
		return map;
	}

	/**
	 * 根据租借订单ID删除一条数据
	 * @param bedmessageId 传入租借订单ID
	 * @return 返回Boolean
	 */
	@Override
	public Boolean deleteBedMessage(Long bedmessageId) {
		SesBedMessage sesBedMessage = sesBedMessageDao.selectByPrimaryKey(bedmessageId);
		Boolean b = sesBedTypeDao.deleteByPrimaryKey(sesBedMessage.getBedBedTypeid()) == 1;
//		Boolean b = sesBedMessageDao.deleteByPrimaryKey(bedmessageId) == 1;
		return b;
	}

	/**
	 * 修改床位开关机状态
	 * @param bedtypeStatus 传入要修改为几号状态,3为关机,0为闲
	 * @param bedtypeId 传入要修改的床位ID
	 * @return 返回对应的状态数字,如果失败返回-1
	 */
	@Override
	public Integer updateOff(Integer bedtypeStatus, Integer bedtypeId) {
		SesBedType sesBedType = new SesBedType();
		sesBedType.setBedtypestatus(bedtypeStatus);
		SesBedTypeExample sesBedTypeExample = new SesBedTypeExample();
		SesBedTypeExample.Criteria criteria = sesBedTypeExample.createCriteria();
		if(bedtypeStatus == 3 || bedtypeStatus == 0) {
		 	criteria.andBedtypeidEqualTo(Long.valueOf(bedtypeId));
			int i = sesBedTypeDao.updateByExampleSelective(sesBedType, sesBedTypeExample);
			if(i == 0) {
				return -1;
			}
			SesBedType bedType = sesBedTypeDao.selectByPrimaryKey(Long.valueOf(bedtypeId));
			return bedType.getBedtypestatus();
		}
		return -1;
	}

	/**
	 * 根据传入的数据新增一条床位订单
	 * @param sesBedMessage 传入对象
	 * @param sesBedType 传入对象
	 * @return 返回Boolean
	 */
	@Override
	public Boolean insertBedMessage(SesBedMessage sesBedMessage, SesBedType sesBedType) {
		if(sesBedMessage == null || sesBedType == null) {
			return false;
		}
		String id = UUID.randomUUID().toString();
		sesBedType.setBedtypemodel(id);
		sesBedType.setBedtypetime(FindDate.getDate());
		sesBedType.setBedtypestatus(0);
		sesBedType.setBedtypebatterypower("100%");
		sesBedType.setBedtypecode(id);
		sesBedType.setBedtypestatustime(FindDate.getDate());
		sesBedType.setBedtypeinventory(1L);
		boolean b = sesBedTypeDao.insertSelective(sesBedType) == 1;
		if(!b) {
			return false;
		}
		SesBedTypeExample sesBedTypeExample = new SesBedTypeExample();
		sesBedTypeExample.createCriteria().andBedtypecodeEqualTo(id);
		List<SesBedType> list = sesBedTypeDao.selectByExample(sesBedTypeExample);
		sesBedMessage.setBedBedTypeid(list.get(0).getBedtypeid());

		boolean c = sesBedMessageDao.insert(sesBedMessage) == 1;
		return c;
	}

	/**
	 * 根据医院ID查询所有的科室
	 * @param hospitalinfoid 传入医院ID
	 * @return 返回SesHospitalinfo
	 */
	@Override
	public SesHospitalinfo findById(Long hospitalinfoid) {
		SesHospitalinfoExample sesHospitalinfoExample = new SesHospitalinfoExample();
		SesHospitalinfoExample.Criteria criteria = sesHospitalinfoExample.createCriteria();
		criteria.andHospitalinfoidEqualTo(hospitalinfoid);
		List<SesHospitalinfo> sesHospitalinfoList = sesHospitalinfoDao.selectByExample(sesHospitalinfoExample);
		for (SesHospitalinfo s: sesHospitalinfoList
		) {
			SesHospitalinfoDepartmentExample sesHospitalinfoDepartmentExample = new SesHospitalinfoDepartmentExample();
			SesHospitalinfoDepartmentExample.Criteria criteria1 = sesHospitalinfoDepartmentExample.createCriteria();
			criteria1.andDepartmentHospitalinfoidEqualTo(hospitalinfoid);
			s.setKeShi(sesHospitalinfoDepartmentDao.selectByExample(sesHospitalinfoDepartmentExample));
		}
		return sesHospitalinfoList.get(0);
	}




}
